package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.service.AdminAddressService;
import com.cskaoyan.market.service.AdminAddressServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/address/*")
public class AdminAddressController extends HttpServlet {

    AdminAddressService adminAddressService = new AdminAddressServiceImpl();

    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        String op = uri.replace(req.getContextPath() + "/admin/address/", "");
        if ("list".equals(op)) {
            list(req, resp);
        }
    }

    private void list(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {

        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String userId = req.getParameter("userId");
        String name = req.getParameter("name");

        List<MarketAddress> marketAddressList = adminAddressService.list(page, limit, sort, order, userId, name);

        Object o = ResponseUtil.okList(marketAddressList);
        String s = JacksonUtil.writeValueAsString(o);
        resp.getWriter().println(s);

    }

    @Override
    protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {

    }
}
